home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 1999 November / SGI IRIX 6.5 Applications 1999 November.iso / dev / java_dev.idb / usr / demos / java / java-and-openGL / gldraw.c.z / gldraw.c
Encoding:
C/C++ Source or Header  |  1998-07-14  |  5.1 KB  |  212 lines

  1. #include <X11/Intrinsic.h>
  2. #include <stdlib.h>
  3.  
  4. #include "MyCanvas.h"
  5. #include "monitor.h"
  6.  
  7. #define AWT_LOCK() awt_lock_enter()
  8. #define AWT_UNLOCK() awt_lock_exit()
  9.  
  10.  
  11. struct ComponentData {
  12.   Widget      widget;
  13.   int         repaintPending;
  14.   int         x1, y1, x2, y2;
  15. };
  16.  
  17. struct CanvasData {
  18.   struct ComponentData        comp;
  19.   Widget                      shell;
  20.   int                         flags;
  21. };
  22.  
  23. void glxmotif(int);
  24.  
  25.  
  26. JNIEXPORT void JNICALL Java_MyCanvas_X11DrawGL
  27. (JNIEnv *env, jobject this, jint pData, jint x0, jint y0, jint x1, jint y1)
  28. {
  29.  
  30.   struct CanvasData *wdata;
  31.   Window w;
  32.   Display *d;
  33.   GC gc;
  34.   XWindowAttributes wattr;
  35.  
  36.   wdata = (struct CanvasData*)pData;
  37.   glxmotif(pData);
  38. }
  39.  
  40. #include <stdio.h>
  41. #include <Xm/Form.h>
  42. #include <Xm/Frame.h>
  43. #include <Xm/DrawingA.h>
  44. #include <X11/keysym.h>
  45. #include <GL/gl.h>
  46. #include <GL/glu.h>
  47. #include <GL/glx.h>
  48. #include <X11/GLw/GLwMDrawA.h>  /* Motif OpenGL drawing area. */
  49.  
  50.  
  51. static int snglBuf[] = {GLX_RGBA, GLX_DEPTH_SIZE, 16, None};
  52. static int dblBuf[] = {GLX_RGBA, GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER, None};
  53.  
  54. Display        *dpy;
  55. GLboolean       doubleBuffer = GL_TRUE, viewportUpdateNeeded = GL_TRUE, spinning = GL_FALSE;
  56. XtAppContext    app;
  57. XtWorkProcId    workId = 0;
  58. Widget          toplevel, form, frame, glxarea;
  59. int gwidth, gheight;
  60.  
  61. void
  62. updateViewport(Widget w)
  63. {
  64.   Dimension width, height;
  65.  
  66.   AWT_LOCK();
  67.   XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, NULL);
  68.   glViewport(0, 0, (GLint) width, (GLint) height);
  69.   viewportUpdateNeeded = GL_FALSE;
  70.   AWT_UNLOCK();
  71.  
  72. }
  73.  
  74. void
  75. draw(Widget w)
  76. {
  77.   if (viewportUpdateNeeded) updateViewport(w);
  78.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  79.   glBegin(GL_POLYGON);
  80.   glColor3f(0.0, 0.0, 0.0); glVertex3f(-10.0, -10.0, 0.0);
  81.   glColor3f(0.7, 0.7, 0.7); glVertex3f(10.0, -10.0, 0.0);
  82.   glColor3f(1.0, 1.0, 1.0); glVertex3f(-10.0, 10.0, 0.0);
  83.   glEnd();
  84.   glBegin(GL_POLYGON);
  85.   glColor3f(1.0, 1.0, 0.0); glVertex3f(0.0, -10.0, -10.0);
  86.   glColor3f(0.0, 1.0, 0.7); glVertex3f(0.0, -10.0, 10.0);
  87.   glColor3f(0.0, 0.0, 1.0); glVertex3f(0.0, 5.0, -10.0);
  88.   glEnd();
  89.   glBegin(GL_POLYGON);
  90.   glColor3f(1.0, 1.0, 0.0); glVertex3f(-10.0, 6.0, 4.0);
  91.   glColor3f(1.0, 0.0, 1.0); glVertex3f(-10.0, 3.0, 4.0);
  92.   glColor3f(0.0, 0.0, 1.0); glVertex3f(4.0, -9.0, -10.0);
  93.   glColor3f(1.0, 0.0, 1.0); glVertex3f(4.0, -6.0, -10.0);
  94.   glEnd();
  95.   if (doubleBuffer) {
  96.     AWT_LOCK();
  97.     glXSwapBuffers(dpy, XtWindow(w));
  98.     AWT_UNLOCK();
  99.   }
  100.   glFlush();
  101. }
  102.  
  103. void
  104. expose(Widget w, XtPointer clientData, XtPointer callData)
  105. {
  106.   AWT_LOCK();
  107.   draw(w);
  108.   AWT_UNLOCK();
  109. }
  110.  
  111. void
  112. resize(Widget w, XtPointer clientData, XtPointer callData)
  113. {
  114.   XmDrawingAreaCallbackStruct *cd = (XmDrawingAreaCallbackStruct *) callData;
  115.   
  116.   AWT_LOCK();
  117.   /* don't try OpenGL until window is realized! */
  118.   if (XtIsRealized(w)) updateViewport(w);
  119.   else viewportUpdateNeeded = GL_TRUE;
  120.   AWT_UNLOCK();
  121.  
  122. }
  123.  
  124. void
  125. glxmotif(int pData)
  126. {
  127.   XVisualInfo    *vi;
  128.   Colormap        cmap;
  129.   GLXContext      cx;
  130.   int             saved_argc;
  131.   String         *saved_argv;
  132.  
  133.   struct CanvasData *wdata;
  134.   Window w;
  135.   Display *d;
  136.   GC gc;
  137.   XWindowAttributes wattr;
  138.  
  139.   wdata = (struct CanvasData*)pData;
  140.   toplevel = wdata->comp.widget;
  141.  
  142.   AWT_LOCK();
  143.  
  144.   dpy = XtDisplay(wdata->comp.widget);
  145.  
  146.   /* find an OpenGL-capable RGB visual with depth buffer */
  147.   vi = glXChooseVisual(dpy, DefaultScreen(dpy), dblBuf);
  148.   if (vi == NULL) {
  149.     vi = glXChooseVisual(dpy, DefaultScreen(dpy), snglBuf);
  150.     if (vi == NULL) XtAppError(app, "no RGB visual with depth buffer");
  151.     doubleBuffer = GL_FALSE;
  152.   }
  153.   cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual, AllocNone);
  154.  
  155.   XGetWindowAttributes(dpy, XtWindow(toplevel),
  156.                &wattr);
  157.  
  158.   gwidth = wattr.width;
  159.   gheight = wattr.height;
  160.  
  161.   form = XmCreateForm(toplevel, "form", NULL, 0);
  162.   XtManageChild(form);
  163.  
  164.   XtVaSetValues(form, XtNwidth, gwidth, XtNheight, gheight, NULL);
  165.  
  166.   frame = XmCreateFrame(form, "frame", NULL, 0);
  167.   XtVaSetValues(frame, XmNbottomAttachment, XmATTACH_FORM,
  168.         XmNtopAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM,
  169.         XmNrightAttachment, XmATTACH_FORM, NULL);
  170.   XtManageChild(frame);
  171.  
  172.   XtVaSetValues(frame, XtNwidth, gwidth, XtNheight, gheight, NULL);
  173.  
  174.   glxarea = XtVaCreateManagedWidget("glxarea",
  175.                     glwMDrawingAreaWidgetClass, frame,
  176.                     GLwNvisualInfo, vi,
  177.                     XtNwidth, gwidth,
  178.                     XtNheight, gheight,
  179.                     XtNcolormap, cmap,
  180.                     NULL);
  181.  
  182. #if 0
  183.   XtAddCallback(glxarea, XmNexposeCallback, expose, NULL);
  184.   XtAddCallback(glxarea, XmNresizeCallback, resize, NULL);
  185. #endif
  186.  
  187.  
  188.   /* create an OpenGL rendering context */
  189.   cx = glXCreateContext(dpy, vi, /* no display list sharing */ None, /* favor direct */ GL_TRUE);
  190.   if (cx == NULL) 
  191.     XtAppError(app, "could not create rendering context");
  192.  
  193.   if (glXMakeCurrent(dpy, XtWindow(glxarea), cx))
  194.     XtAppError(app, "could not bind OpenGL to rendering context");
  195.  
  196.   /* setup OpenGL state */
  197.   glEnable(GL_DEPTH_TEST);
  198.   glDepthFunc(GL_LEQUAL); 
  199.   glClearDepth(1.0);
  200.   glClearColor(0.0, 0.0, 0.0, 0.0);
  201.   glLoadIdentity();
  202.   gluPerspective(40.0, 1.0, 10.0, 200.0);
  203.   glTranslatef(0.0, 0.0, -50.0); 
  204.   glRotatef(-58.0, 0.0, 1.0, 0.0);
  205.  
  206.   /* draw into the glxarea */
  207.   draw(glxarea);
  208.  
  209.   AWT_UNLOCK();
  210.  
  211. }
  212.